package com.controller;

import com.util.DataSourceUtils;
import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;

import java.io.IOException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.time.LocalDateTime;

@WebServlet("/filter/deletenews")
public class DeleteNewsServlet extends HttpServlet {
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException{
        req.getRequestDispatcher("/WEB-INF/jsp/xianshi.jsp").forward(req,resp);
    }

    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException
    {
        String nid=req.getParameter("nid");
        String sql="DELETE FROM Newss WHERE nid = ?;";
        try (Connection con= DataSourceUtils.getConnection(); PreparedStatement ps = con.prepareStatement(sql) ){
            ps.setString(1, nid);
            ps.executeUpdate();
        }catch (SQLException e) {
            throw new RuntimeException(e);
        }
        resp.sendRedirect(req.getContextPath()+"/filter/houtai");
    }
}
